/**
 * 优化代码分支，尤其是if...else语句过多
 * @param {string} name 动物名字
 * @returns {any} 任何值
 */
function speak(name) {
  /* const map = {
    老牛: () => console.log("老牛默默叫"),
    老虎: () => console.log("写入文件：", "老婆嗷嗷嗷叫"),
    小猫: () => console.log("上传至服务器：", "小猫喵喵叫")
  }; */

  // 写成元组
  const map = [
    [() => name.includes("牛"), () => console.log("老牛默默叫")],
    [() => name.endsWith("虎") && name.length <= 3, () => console.log("写入文件：", "老婆嗷嗷嗷叫")],
    [() => name.endsWith("猫") && !name.includes("狗"), () => console.log("上传至服务器：", "小猫喵喵叫")]
  ];

  /* if (map[name]) {
    map[name]();
  } */
  const target = map.find(m => m[0]());
  if (target) {
    target[1]();
  } else {
    console.log("找不到，不知道怎么叫");
  }
}

// 测试
speak("老虎");
